package com.beuth.taskList.activities.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;

import android.os.Environment;
import android.util.Log;

import com.beuth.taskList.model.Priority;
import com.beuth.taskList.model.Task;
import com.beuth.taskList.model.TaskType;

public class DataIO {
	
	private static FileInputStream fileInputStream; 
	private static FileOutputStream fileOutputStream; 
	private String pathToExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
	//TODO test.txt heisst nur so, damit man die datei auf android telefon findet.. 
	//In der release version auf ".data" oder ".settings" aendern, dann wird sie unsichtbar!
	private String fileName = "test.txt";
	private String dataSeperator = "##";
	private SimpleDateFormat simpleFormat = new SimpleDateFormat ("d M y");
	
	public void load(List<Task> tasks) {
		
		
        BufferedReader in = null;
		try {
			fileInputStream = new FileInputStream(pathToExternalStorage
					+ fileName);
			in = new BufferedReader(new InputStreamReader(fileInputStream));

			String line;
			while ((line = in.readLine()) != null) {
				// Todo anhand des Seperators wieder auseinandernehmen
				Task tempTask = new Task();
				tempTask.setName(line.split(dataSeperator)[0]);

				tempTask.setTaskType(TaskType.getFromString(line
						.split(dataSeperator)[1]));
				tempTask.setPriority(Priority.getFromString(line
						.split(dataSeperator)[2]));

				GregorianCalendar date = new GregorianCalendar();
				try {
					date.setTime(simpleFormat.parse(line.split(dataSeperator)[3]));
				} catch (ParseException e) {
					Log.d("ParseTimeFailed", e.toString());
				}

				tempTask.setClosingDate(date);
				tasks.add(tempTask);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
    
    public void save(List<Task> tasks) {
    	
        BufferedWriter out = null;
		try{
			fileOutputStream = new FileOutputStream(pathToExternalStorage + fileName);
			out = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
			
				for (Task task : tasks) {
					out.write(task.getName()+ dataSeperator);
					out.write(task.getTaskType().toString()+ dataSeperator);
					out.write(task.getPriority().toString()+ dataSeperator);

					out.write(simpleFormat.format(task.getClosingDate().getTime()) + dataSeperator);
					out.write("\n");
				}
		} 
		catch(IOException e){
			e.printStackTrace();
		}
		finally {
			try{
				if(out!=null) out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}  
    }
	
	
	
	
	
}
